package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.chlid

import android.os.Bundle
import android.util.Log
import android.view.View
import com.example.library_common.base.BaseLazyDataBindingFragment
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.FragmentAgencyApplyingBinding
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.AgencySubPageInfo
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AreaItemBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.config.*
import com.example.shushiyunsst.util.HiDataBus
import com.google.android.material.tabs.TabLayout
import com.jzp.rotate3d.Rotate3D
import kotlinx.android.synthetic.main.for_lending_fragment.*

/**
 *申抢代理
 * */
class AgencyApplyingFragment : BaseLazyDataBindingFragment<FragmentAgencyApplyingBinding>(),
    AgencySubPageInfo, TabLayout.OnTabSelectedListener {

    override val title = R.string.agency_applying

    override val icon = R.mipmap.agency_applying

    companion object {
        fun newInstance(): AgencyApplyingFragment {
            val args = Bundle()
            val fragment = AgencyApplyingFragment()
            fragment.arguments = args
            return fragment
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    private var rotate: Rotate3D? = null

    private val tabs = mapOf(
        PROVINCE to "省列表",
        CITY to "市列表",
        DISTRICT to "区县列表"
    )


    private val mRestFragment = AgencyApplyingRestFragment.newInstance()

    private val mAreaFragment = AgencyApplyingAreaFragment.newInstance()

    override fun layoutId(): Int {
        return R.layout.fragment_agency_applying
    }

    override fun lazyInit() {


        rotate = Rotate3D.Builder(_mActivity)
            .setParentView(viewBinder.panel)
            .setPositiveView(viewBinder.mAreaFrameLayout)
            .setNegativeView(viewBinder.mRestFrameLayout)
            .create()

    }

    override fun initView(view: View) {

        loadRootFragment(R.id.mAreaFrameLayout, mAreaFragment)
        loadRootFragment(R.id.mRestFrameLayout, mRestFragment)


        for (e in tabs.entries) {
            viewBinder.mTabLayout.addTab(
                viewBinder.mTabLayout.newTab()
                    .setText(e.value)
                    .setTag(e.key)
            )

        }

        val selectedTabPosition = viewBinder.mTabLayout.selectedTabPosition

        val tab = viewBinder.mTabLayout.getTabAt(selectedTabPosition)
        mAreaFragment.mTab = tab

        nextTabClickable(selectedTabPosition)

        viewBinder.mTabLayout.addOnTabSelectedListener(this)

        onEvent()
    }

    private fun onEvent() {

        HiDataBus.with<AreaItemBean>(AGENCY_APPLYING_AREA_Event)
            .observerSticky(this, false, {
                if (rotate?.isOpen == false) {
                    rotate?.transform()
                }

                val currentPosition = viewBinder.mTabLayout.selectedTabPosition
                val itemTab = viewBinder.mTabLayout.getTabAt(currentPosition)
                itemTab?.text = it?.areaName

                nextTabClickable(currentPosition + 1)
            })

        HiDataBus.with<String>(AGENCY_APPLYING_REST_BACK_TO_AREA_LIST)
            .observerSticky(this, false, {
                if (rotate?.isOpen == true) {
                    rotate?.transform()
                }
            })

        HiDataBus.with<String>(AGENCY_APPLYING_BACK_TO_INIT)
            .observerSticky(this, false, {
                if (rotate?.isOpen == true) {
                    rotate?.transform()
                }
                mAreaFragment.selectedItem = null

                viewBinder.mTabLayout.selectTab(viewBinder.mTabLayout.getTabAt(0))

                val tabCount = viewBinder.mTabLayout.tabCount

                for (i in 0 until tabCount) {
                    val tabAt = viewBinder.mTabLayout.getTabAt(i)
                    tabAt?.text = tabs[tabAt?.tag]
                    tabAt?.view?.isClickable = i == 0
                }

                HiDataBus.with<String>(AGENCY_APPLYING_INTENT_TO_AGENCY_MY_SPOT)
                    .postStickyData("")
            })
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        if (rotate?.isOpen == true) {
            rotate?.transform()

        }
        mAreaFragment.mTab = tab
        mAreaFragment.onRefresh()

//        if (mAreaFragment.isHaveSelected){
//            val nextTabPos = (tab?.position ?: 0) + 1
//            nextTabClickable(nextTabPos)
//        }

    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
//            val nextTabPos = (tab?.position ?: 0) + 1

//        val current = tab?.position ?: 0
        val selectedTabPosition = viewBinder.mTabLayout.selectedTabPosition
//        Log.d("onTabUnselected", "onTabUnselected: ${tab?.position}")
//        Log.d("onTabUnselected", "selectedTabPosition: $selectedTabPosition")
//        if (current < selectedTabPosition) return
        val tabCount = viewBinder.mTabLayout.tabCount
        for (i in 0 until tabCount) {
            val tabAt = viewBinder.mTabLayout.getTabAt(i)
            if (i > selectedTabPosition) {
                tabAt?.text = tabs[tabAt?.tag]
            }
        }
//        nextTabClickable(current)
//        tab?.text = tabs[tab?.tag]
    }

    override fun onTabReselected(tab: TabLayout.Tab?) {
        if (rotate?.isOpen == true) {
            rotate?.transform()

        }
        mAreaFragment.mTab = tab
        mAreaFragment.onRefresh()

//        if (mAreaFragment.isHaveSelected){
//            val nextTabPos = (tab?.position ?: 0) + 1
//            nextTabClickable(nextTabPos)
//        }

    }

    private fun nextTabClickable(nextTabPos: Int?) {
        val tabCount = viewBinder.mTabLayout.tabCount
        val nextTabTempPos = nextTabPos ?: 0
        for (i in 0 until tabCount) {
            val tabAt = viewBinder.mTabLayout.getTabAt(i)
            tabAt?.view?.isClickable = i <= nextTabTempPos
            if (i >= nextTabTempPos) {
                tabAt?.text = tabs[tabAt?.tag]
            }
        }
    }

//    override fun onDestroy() {
//        super.onDestroy()
//        viewBinder.mTabLayout.removeAllTabs()
//        viewBinder.mTabLayout.removeOnTabSelectedListener(this)
//    }

}